<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>更新用户</title>
		<script type="text/javascript" src="/js/jquery-1.11.1.js"></script>
		<script type="text/javascript" src="/js/bootstrap.js"></script>
		<link rel="stylesheet" type="text/css" href="/css/bootstrap.css"/>
		<link rel="stylesheet" type="text/css" href="/css/style.css"/>
		<style>
			label span{color: #ff0000; padding-right: 5px;}
		</style>
	</head>

	<body>
		<h2>更新用户</h2>
		<form id="myForm" class="form-horizontal">
			<div class="form-group">
				<label class="col-sm-1 control-label"><span>*</span>用户编号</label>
				<div class="col-sm-9">
					<input required="required" name="u_id" class="form-control" placeholder="请输入 用户id" disabled/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label"><span>*</span>登录账号</label>
				<div class="col-sm-9">
					<input required="" name="u_logInName" class="form-control" placeholder="请输入 登录的账号"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label"><span>*</span>登录密码</label>
				<div class="col-sm-9">
					<input required="" name="u_passWord" class="form-control" type="password"/>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-1 control-label">姓名</label>
				<div class="col-sm-9">
					<input name="u_name" class="form-control" placeholder="请输 用户真实姓名"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">邮箱</label>
				<div class="col-sm-9">
					<input name="u_email" class="form-control" placeholder="请输入 用户邮箱"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">联系方式</label>
				<div class="col-sm-9">
					<input name="u_phone" class="form-control" placeholder="请输入 联系方式"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">用户状态</label>
				<div class="col-sm-2">
					<select name="u_state" class="form-control">
						<option value="-1">是否禁用</option>
						<option value="0">正常</option>
						<option value="1">禁用</option>
					</select>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-1 control-label"></label>
				<div class="col-sm-9">
					<button id="btnUpdate" type="button" class="btn btn-sm btn-info">提&nbsp;交</button>
					&nbsp;&nbsp;&nbsp;&nbsp;
				</div>
			</div>
		</form>		
	</body>
</html>
<script>
	String.prototype.GetValue= function(para) {
		let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
		let r = this.substr(this.indexOf("\?")+1).match(reg);
		if (r!=null) return unescape(r[2]); return null;
	}
	$(function () {
		//根据地址获取teamId,如果null则执行增加，否则执行更新
		let url = document.location.toString();//获取URL
		let u_num=url.GetValue("u_num");
		let pageNum=url.GetValue("pageNum");
		let pageSize=url.GetValue("pageSize");
		//alert(teamId);
		//根据id查询要更新的球队信息并回显到页面
		queryById(u_num);
		//提交按钮的单击事件--实现真正的更新
		$("#btnUpdate").click(function () {
			$.ajax({
				type: "POST",//这里必须写POST
				url: "/user/",
				data: $("#myForm").serialize()+"&_method=PUT"+"&u_num="+u_num,
				dataType:"json",
				success: function(vo){
					if(vo.code==200) {
						alert("操作成功！");
						window.location.href = "/pages/user/list.html?";
					}else{
						alert("更新失败！"+vo.msg);
					}
				}
			});
		});
	});
	function queryById(u_num) {
		$.ajax({
			type: "GET",
			url: "/user/"+u_num, //RESTful风格的API定义
			data: "",
			success: function (vo) {
				let obj=vo.obj;
				let u_id=obj.u_id;
				let u_logInName=obj.u_logInName==null?'':obj.u_logInName;
				let u_name=obj.u_name==null?'':obj.u_name;
				let u_email=obj.u_email==null?'':obj.u_email;
				let u_phone=obj.u_phone==null?'':obj.u_phone;
				let u_state=obj.u_state;
				$("input[name='u_id']").val(u_id);
				$("input[name='u_logInName']").val(u_logInName);
				$("input[name='u_name']").val(u_name);
				$("input[name='u_email']").val(u_email);
				$("input[name='u_phone']").val(u_phone);
				$("select[name='u_state']").val(u_state);
			}
		});
	}

</script>